﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ControlAdapters.Renderers;

namespace HelfenHelfen.Web.Forms.ControlAdapters {
   /// <summary>
   /// Renders a <see cref="Menu"/> using HTML markup.
   /// </summary>
   public class MenuHtmlRenderer : HtmlRenderer<Menu> {
      /// <summary>
      /// Initializes a new instance of the class.
      /// </summary>
      /// <param name="control">The <see cref="Menu"/> control that this class will render.</param>
      public MenuHtmlRenderer(Menu control)
         : base(control) {
      }

      /// <summary>
      /// Renders the beginning tag that wraps around the rendered HTML.
      /// </summary>
      /// <returns>The beginning tag HTML code.</returns>
      public override string RenderBeginTag() {
         HtmlTextWriter writer = CreateHtmlTextWriter();
         writer.WriteLine("<ul class=\"menu\">");
         return writer.InnerWriter.ToString();
      }


      /// <summary> 
      /// Finds a Control recursively. Note finds the first match that exists 
      /// </summary> 
      /// <param name="ContainerCtl">Should be the lowest container in the heirarchy, for eg dont choose Master page if you can pick the specific panel</param> 
      /// <param name="IdToFind">ID of the control you are looking for</param> 
      /// <returns>the control if found else null</returns> 
      private static Control FindControlRecursive(Control Root, string Id) {
         if (Root.ID == Id)
            return Root;
         foreach (Control Ctl in Root.Controls) {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
               return FoundCtl;
         }

         return null;
      }


      protected void walkTree(SiteMapNode pNode, HtmlTextWriter writer) {
         foreach (SiteMapNode child in pNode.ChildNodes) {
            if ((child.Title.Length > 0) && (child.Url.Length > 0)) {
               writer.WriteLine("      <li><a href=\"" + child.Url.ToString() + "\">" + child.Title.ToString() + "</a></li>");
            }
         }
      }


      /// <summary>
      /// Renders inner HTML code representing the adapted control.
      /// </summary>
      /// <returns>The inner HTML code representing the adapted control.</returns>
      public override string RenderContents() {
         HtmlTextWriter writer = CreateHtmlTextWriter();

         var src = (SiteMapDataSource)FindControlRecursive(base.Control.Page, Control.DataSourceID);
         var src2 = SiteMap.Provider;
         src.Provider = src2;

         //    DataSourceView view = src.GetView();


         foreach (SiteMapNode cNode in SiteMap.RootNode.ChildNodes) {
            writer.WriteLine("<li class=\"level1\">");
            writer.WriteLine("   <div class=\"title\">");
            writer.WriteLine("      " + cNode.Title.ToString());
            writer.WriteLine("   </div>");
            writer.WriteLine("   <ul>");
            walkTree(cNode, writer);
            writer.WriteLine("   </ul>");
            writer.WriteLine("</li> <!-- .level1 -->");

         }
         return writer.InnerWriter.ToString();
      }


      /// <summary>
      /// Renders the ending tag that closes the tag generated by <see cref="RenderBeginTag"/>.
      /// </summary>
      /// <returns>The ending tag HTML code.</returns>
      public override string RenderEndTag() {
         HtmlTextWriter writer = CreateHtmlTextWriter();
         writer.WriteLine("</ul> <!-- .menu -->");
         return writer.InnerWriter.ToString();
      }
   }

}
